/*

   Web2Pano beta

   Copyright IstraSoft Labs 2006

   www.istrasoft.be

*/

  var Web2Pano = new Class({

	  initialize: function(params){
      this.cfg = params;
      this.dom = {};
      this.vars = {};
      this._addToDOM();
	  },

	  load : function(file, title)
	  {
	    if(this.dom)
	    {
	      this.dom.bar.innerHTML = title;
  	    this.dom.msg.innerHTML = '[ Panorama loading ... ]';
  	    this.dom.msg.show();
        this.vars.preloader.src = file;
      }
	  },

	  play : function(dir)
	  {
      this.vars.offset = (dir == 'left') ? 2 : -2;
      this._scroll();
	  },

	  /**************************/

	  _addToDOM : function(){

      this.dom.main   = new Element('div').setId('main');
      this.dom.mask   = new Element('div').setId('mask');
      this.dom.pano   = new Element('div').setId('pano');
      this.dom.shield = new Element('div').setId('shield');
      this.dom.msg    = new Element('div').setId('msg');
      this.dom.left   = new Element('div').setId('left');
      this.dom.right  = new Element('div').setId('right');
      this.dom.bar    = new Element('div').setId('bar');

      this.dom.main.appendChild(this.dom.mask);
      this.dom.main.appendChild(this.dom.bar);
        this.dom.mask.appendChild(this.dom.pano);
        this.dom.mask.appendChild(this.dom.shield);
          this.dom.shield.appendChild(this.dom.msg);
        this.dom.mask.appendChild(this.dom.left);
        this.dom.mask.appendChild(this.dom.right);


      this.vars.preloader = $(new Image);

      this.vars.preloader.addEventEx('load', this, function()
      {
        this.dom.msg.hide();
        this.vars.pic_w  = this.vars.preloader.width;
        this.vars.mask_w = this.dom.mask.getPos('width');
        this.vars.min_l  = this.vars.mask_w - this.vars.pic_w * 3;
        this.dom.pano.setPos('width', this.vars.pic_w * 3);
        this.dom.pano.setStyle('background', 'url(' + this.vars.preloader.src + ')');
        this.dom.pano.setPos('left', Math.round(this.vars.mask_w / 2) -
                                     this.vars.pic_w -
                                     Math.round(this.vars.pic_w / 2));
      });


      this.dom.pano.style.zIndex   = this.cfg.base_zIndex;
      this.dom.shield.style.zIndex = this.cfg.base_zIndex + 1;
      this.dom.left.style.zIndex   = this.cfg.base_zIndex + 2;
      this.dom.right.style.zIndex  = this.cfg.base_zIndex + 2;

      this.dom.msg.innerHTML = '[ No panorama loaded ]';
      this.dom.bar.innerHTML = 'Web2Pano by IstraSoft Labs';

      $(this.cfg.container).appendChild(this.dom.main);

      this.dom.left.addEventEx('mouseover', this, function()
      {
        this.vars.offset = 4;
        this._scroll();
      });
      this.dom.right.addEventEx('mouseover', this, function()
      {
        this.vars.offset = -4;
        this._scroll();
      });
      this.dom.left.addEventEx('mouseout', this, function()
      {
        this._stopScroll();
      });
      this.dom.right.addEventEx('mouseout', this, function()
      {
        this._stopScroll();
      });
      this.dom.shield.addEventEx('mouseover', this, function()
      {
        this._stopScroll();
      });

	  },

	  _scroll : function()
	  {
	    if(this.vars.scrollTimer)
	    {
        var cur_l = Number(this.dom.pano.getPos('left')) + this.vars.offset;
        if(cur_l > -200)
        {
          cur_l = cur_l - this.vars.pic_w;
        }
        else if(cur_l - this.vars.min_l < 200)
        {
          cur_l = cur_l + this.vars.pic_w;
        }
        this.dom.pano.setPos('left', cur_l);
	    }
	    else
	    {
	      this.vars.scrollTimer = this._scroll.periodical(10, this);
	    }
	  },

	  _stopScroll : function()
	  {
	    if(this.vars.scrollTimer)
	    {
	      $clear(this.vars.scrollTimer);
	      this.vars.scrollTimer = false;
	    }
	  }

  });

  Element.extend({

  	addEventEx: function(action, obj, fn){
  	  fn = fn.bind(obj);
  		this[action+fn] = fn;
  		if (this.addEventListener) this.addEventListener(action, fn, false);
  		else this.attachEvent('on'+action, this[action+fn]);
  		var el = this;
  		if (this != window) Unload.functions.push(function(){
  			el.removeEvent(action, fn);
  			el[action+fn] = null;
  		});
  		return this;
  	},

    setId : function(id)
    {
      this.id = 'w2p_' + id;
      return this;
    },

    show : function()
    {
      this.style.display = 'block';
      return this;
    },

    hide : function()
    {
      this.style.display = 'none';
      return this;
    },

    getPos : function(kind)
    {
      return this.getStyle(kind).replace(/(px|pt)/,'');
    },

    setPos : function(kind, val)
    {
      return this.setStyle(kind, val + 'px');
    }

  });